package com.dshore.messagecenter.controller.topicqueue;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dshore.messagecenter.model.system.ResponseResult;
import com.dshore.messagecenter.service.topicQueue.QueueAccessService;
import com.dshore.messagecenter.service.topicQueue.ServiceAccessService;
import com.dshore.messagecenter.service.topicQueue.TopicQueueService;
import com.dshore.messagecenter.util.UUIDUtil;

@Controller
@RequestMapping("/queueAccess/")
public class QueueAccessController {
	
//	private Logger logger = LoggerFactory.getLogger(QueueAccessController.class);
	@Autowired
	private QueueAccessService accessService;
	@Autowired
	private ServiceAccessService serviceAccessService;
	@Autowired
	private TopicQueueService topicQueueService;
	/**
	 * 点击新增按钮进入新增页面
	 */
	@RequestMapping(value= "addQueue",method = RequestMethod.POST)
	@ResponseBody
	public String addQueue(HttpSession session) {
		//创建map
		Map<String, Object> map = new HashMap<>();
		String key = UUIDUtil.getUUId();
		session.setAttribute(key, map);
		return key;
	}
	
	
	/**
	 * 点击修改时获取数据库数据放入session
	 * @param session
	 * @param id
	 * @param topicQueueNo
	 * @param key
	 */
	@RequestMapping(value= "saveQueueId",method = RequestMethod.POST)
	@ResponseBody
	public String saveQueueId(HttpSession session, String id, String status, String topicQueueNo) {
		//获取数据库数据放入session
		Map<String, Object> map = accessService.getQueueInfo(topicQueueNo,id);
		map.put("tId", id);//主题队列id
//		System.out.println("传入的status: " + status);
		map.put("status", status);
		//修改时存入值区分新增还是修改 edit=1
		map.put("edit", 1);
//		System.out.println("修改时的session: " + map);
		//存入session
		String key = UUIDUtil.getUUId();
		session.setAttribute(key, map);
//		System.out.println("--------------");
		return key;
	}
	/**
	 * 校验目标应用个数
	 */
	/*@RequestMapping(value= "checkLastTar",method = RequestMethod.POST)
	@ResponseBody
	public boolean checkLastTar(int index, String key, HttpSession session) {
		Map<String, Object> map = (Map<String, Object>)session.getAttribute(key);
		
		//目标文件集合
		List<Map<String,String>> targetList = (List<Map<String, String>>) map.get("targetList");
		if(targetList==null) {
			return false;
		}else {
			if(targetList.size()>=index-1) {
				if(targetList.get(index-2).size()==5) {
					return true;
				}else {
					return false;
				}
			}else {
				return false;
			}
		}
	}*/
	
	/**
	 * 根据下标删除目标应用
	 */
	@RequestMapping(value= "delTarApp",method = RequestMethod.POST)
	@ResponseBody
	public void delTarApp(int idIndex, String key, HttpSession session) {
		Map<String, Object> map = (Map<String, Object>)session.getAttribute(key);
//		System.out.println("应用下标: " + idIndex);
		if(map != null) {
			accessService.delTarApp(idIndex,map);
		}
	}
	
	
	/**
	 * 监听input框保存主题队列名称到session中
	 */
	@RequestMapping(value= "saveQueueName",method = RequestMethod.POST)
	@ResponseBody
	public void saveQueueName(String topicQueueName, String key, HttpSession session) {
		Map<String, Object> map = (Map<String, Object>)session.getAttribute(key);
//		System.out.println("topicQueueName: " + topicQueueName);
		if(StringUtils.isNotBlank(topicQueueName) ) {
			map.put("topicQueueName", topicQueueName);
		}
		session.setAttribute(key, map);
		
	}
	
	/**
	 * 查询Session中应用Map
	 */
	@RequestMapping(value= "queryAppMap",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> queryAppMap(String key,HttpSession session) {
		Map<String, Object> map = (Map<String, Object>)session.getAttribute(key);
		return map;
	}
	
	/**
	 * 存取源应用到Session中
	 */
	@RequestMapping(value= "saveOriApp",method = RequestMethod.POST)
	@ResponseBody
	public String saveOriApp(@RequestParam(value = "originId",required = false) String originId,
						@RequestParam(value = "originName",required = false) String originName,
						String key,
						HttpSession session) {
		//获取session中的map
		Map<String, Object> map = (Map<String, Object>)session.getAttribute(key);
		if(map == null) {
			map = new HashMap<>();
		}
//		System.out.println("存取源应用时map集合: " + map);
		map = accessService.saveOriApp(originId,originName,map,session);

		// 将key存到session中
		session.setAttribute(key,map);
		//以下可以删除
//		map = (Map<String, Object>) session.getAttribute(key);
//		System.out.println("更新后获取的Session中数据： " + map);
//		System.out.println("-------------------------");
		
		return key;
	}
	
	/**
	 * 动态存储目标应用到Session中
	 * @param targetId 第一次的目标应用id
	 * @param targetName 
	 * @param tarId 更换时之前的目标应用id
	 * @param key
	 * @param session
	 * @return
	 */
	//String tarId
	@RequestMapping(value= "saveTApp",method = RequestMethod.POST)
	@ResponseBody
	public ResponseResult<Map<String, Object>> saveTApp(String targetId,String targetName,String index,
			String key,HttpSession session) {
//		System.out.println("更换前目标应用下标index: " + index);
//		System.out.println("更换前目标应用的tarId: " + tarId);
		//存取目标应用集合
		return accessService.saveTApp(key,targetId,targetName,index,session);
		
	}
	
	
	
	
	
	/**
	 * 新增/修改 消息队列接入页面
	 * @param session
	 * @param key
	 * @param id ， 队列id 用于修改用
	 * @param topicQueueNo 队列编码
	 * @return
	 */
	@RequestMapping("page")
	public ModelAndView page(HttpSession session, 
			@RequestParam(value = "key",required = false) String key) {
		ModelAndView mv = new ModelAndView("accessType/queueAccess");
		
		//Session中map
		Map<String, Object> map = (Map<String, Object>) session.getAttribute(key);
//		System.out.println("接入页面的Session中map： " + map);
		
		List<Map<String,String>> targetList = new ArrayList<>();
		String status = "";
		//目标应用集合
		if(map != null) {
			if (map.containsKey("targetList")) {
				//取值targetList
				targetList = (List<Map<String, String>>) map.get("targetList");
			}
			if(map.get("status") !=null) {
				status = map.get("status").toString();
			}
		}
		
//		System.out.println("发布状态: " + status);
		//Session有值时取原有key
		mv.addObject("status",status);
		mv.addObject("key", key);
		mv.addObject("map", map);//key对应的value
		
		int targetNum =  targetList.size();
		mv.addObject("targetNum", targetNum);//目标应用个数
		if(targetNum > 0) {
			mv.addObject("targetList", targetList);
		}
		
		return mv;
	}
	/**
	 * 新增时验证主题队列名称是否重复
	 * @param name 主题队列名称
	 * @param session
	 * @param key
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/checkName")
	@ResponseBody
	public boolean checkName(String topicQueueName,HttpSession session,String key ) {
		Map<String, Object> map = (Map<String, Object>)session.getAttribute(key);
		// 校验名称不重复
		String id=null;
		if(map==null) {
			id="";
		}else {
			id=(String)map.get("tId");
//			System.out.println("主题队列id: " + id);
			if(id==null) {
				id="";
			}
		}
		if(Pattern.compile("^[a-zA-Z0-9\\u4e00-\\u9fa5]{1,32}+$").matcher(topicQueueName).find()) {
			boolean flag = topicQueueService.checkAppName(topicQueueName,id);
			if(!flag) {
				return false;
			}
		}
		return true;
	}
}
